public with sharing class HCPSetEffectiveDateExt {
    String choice = null;
    String hcpId = System.currentPageReference().getParameters().get('id');
    List<HCP__c> target = [select Name, approvaltype__c, effectivedate__c, approvalstatus__c, Active__c, class3__c, class2__c, class1__c, changetoclass1__c, changetoclass2__c, classchangeto3__c, comments__c from HCP__c where Id =: System.currentPageReference().getParameters().get('id')];
    
    public HCP__c hcp {
        get{
            if (hcp==null)
                if(target.size() > 0){
                    hcp = target[0];
                    Date tmp = date.today();
                    if(tmp.month() <> 12)
                        hcp.effectivedate__c = date.newinstance(tmp.year(), tmp.month()+1, 1);
                    else
                        hcp.effectivedate__c = date.newinstance(tmp.year()+1, 1, 1);
                }
                else
                    hcp = null;
            return hcp;
        }
        set;
    }
    
    public PageReference Submit() {
        if(hcp.approvalstatus__c <> '审批通过但未生效')
            hcp.addError('非法操作：无需设置生效日期');
        else if(hcp.effectivedate__c <= date.today())
            hcp.addError('生效日期无效');
        else{
            update hcp;
            return new ApexPages.StandardController(new HCP__c(Id = hcpId)).view();
        }
        return null;
    }
    
    public PageReference Cancel() {
        return new ApexPages.StandardController(new HCP__c(Id = hcpId)).view();
    }
    
    static testMethod void HCPSetEffectiveDateExtTest()
    {
        HCPSetEffectiveDateExt test = new HCPSetEffectiveDateExt();
        HCP__c add = new HCP__c();
        test.target.add(add);

        test.Submit();
        test.Cancel();
    }    
}